﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemSettings.CommonlyUsedSettings;
using SystemCenterSoa.PoModel.SystemSettings.CommonlyUsedSettings;
using SystemCenterSoa.Repository.CommonLib;
using Microsoft.EntityFrameworkCore;
using Dapper;
using System.Data;
using System.Data.SqlClient;

namespace SystemCenterSoa.Repository.SystemSettings.CommonlyUsedSettings
{
    public class DictionaryInstallRepository : BaseRepository, IDictionaryInstallRepository
    {
        
        //注入ef的的上下文
        public DictionaryInstallRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }
        public int DictionaryInstallAdd(DictionaryInstallPo info)
        {
            if (string.IsNullOrWhiteSpace(info.dictionary_name))
            {
                return 0;
            }
            _systemCenterDbSoaContext.DictionaryInstallPo.Add(info);           
            return _systemCenterDbSoaContext.SaveChanges();
        }

        public int DictionaryInstallDelete(List<int> ids)
        {
            foreach(var id in ids)
            {
               var data=_systemCenterDbSoaContext.DictionaryInstallPo.Remove(_systemCenterDbSoaContext.DictionaryInstallPo.Find(id));
            }
         
            return _systemCenterDbSoaContext.SaveChanges();
        }

        public DictionaryInstallPo DictionaryInstallEdit(int id)
        {
            return _systemCenterDbSoaContext.DictionaryInstallPo.Find(id);
        }

        public int DictionaryInstallUpdate(DictionaryInstallPo info)
        {
            if (string.IsNullOrWhiteSpace(info.dictionary_name))
            {
                return 0;
            }
            _systemCenterDbSoaContext.Entry(info).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges();
        }

        //获取下拉
        public List<DictionaryInstallPo> GetDropDown()
        {
            string sql = "select distinct dictionary_type   from dictionary_install";
            using(SqlConnection conn=new SqlConnection(Config))
            {
                conn.Open();
                var list = conn.Query<DictionaryInstallPo>(sql).ToList();
                return list;
            }
           
        }

        public PagerResult<DictionaryInstallPo> SearchPage(SearchModel searchmodel)
        {
            var query = _systemCenterDbSoaContext.DictionaryInstallPo.AsQueryable();
            //判断searchmodel内是否有条件
            if (searchmodel.SearchItem != null && searchmodel.SearchItem.Count() > 0)
            {
                //取出所有数据
                Dictionary<string, string> tempDi = searchmodel.SearchItem;

                string name = "dictionary_name";
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    query = query.Where(c => c.dictionary_name.Contains(tempDi[name]));
                }

                string status = "dictionary_type";
                if (tempDi.Keys.Contains(status) && !string.IsNullOrEmpty(tempDi[status]))
                {                
                        query = query.Where(c => c.dictionary_type==(tempDi[status]));
                }
            }
            return BasePageSearch(query, searchmodel);
        }
    }
}
